package com.jingtai.shop.service.user.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.enums.ProcessStatusEnum;
import com.jingtai.common.enums.UserTypeEnum;
import com.jingtai.common.utils.MyBeanUtils;
import com.jingtai.common.utils.random.TokenUtil;
import com.jingtai.core.entity.shop.Feedback;
import com.jingtai.core.mapper.shop.FeedbackMapper;
import com.jingtai.shop.service.user.FeedbackService;
import com.jingtai.shop.webfacade.bo.feedback.ApplyFeedbackBO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 反馈表 服务实现类
 * </p>
 *
 * @author GR123
 * @since 2020-11-29
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Resource
    private FeedbackService feedbackService;

    /**
     * @param applyContent 内容
     * @param images       图片
     * @param userId       用户id
     * @return true/false
     * @Describe 用户申请反馈
     * @Author GR
     * @date 2020/12/2 0002
     */
    @Override
    public boolean userApplyFeedback(String applyContent, String images, Long userId) {
        Feedback feedback = new Feedback();
        feedback.setImages(images);
        feedback.setApplyContent(applyContent);
        feedback.setUserId(userId);
        feedback.setUserType(UserTypeEnum.USER.getStatus());
        feedback.setApplyState(ProcessStatusEnum.PENDING.getStatus());
        return feedbackService.save(feedback);
    }

    /**
     * @param userId 用户id
     * @return page
     * @Describe 分页查询用户反馈内容
     * 一次分页返回用户的所有反馈信息,前端根据这些信息的回复状态(applyState)去进行分类处理
     * @Author GR
     * @date 2020/12/2 0002
     */
    @Override
    public Page<Feedback> pageFeedback(Long userId, Long pageNo, Long pageSize) {
        return feedbackService.page(new Page<>(pageNo, pageSize), Wrappers.lambdaQuery(Feedback.class)
                .eq(Feedback::getUserId, userId)
                .notIn(Feedback::getApplyState,ProcessStatusEnum.REVOKED.getStatus())
                .eq(Feedback::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
    }

    /**
     * @Describe 用户查询反馈信息内容
     *
     * @Author GR
     * @param id 反馈信息主键id
     * @return feedback
     * @date 2020/12/2 0002
     */
    @Override
    public Feedback queryFeedbackDetails(Long id) {
        return feedbackService.getById(id);
    }

    /**
     * @Describe 用户撤销反馈信息
     *
     * @Author GR
     * @param id 反馈信息主键id
     * @return true/false
     * @date 2020/12/2 0002
     */
    @Override
    public boolean revokeFeedback(Long id) {
        Feedback feedback = new Feedback();
        feedback.setId(id);
        feedback.setApplyState(ProcessStatusEnum.REVOKED.getStatus());
        return feedbackService.updateById(feedback);
    }
}
